import Toast from 'tdesign-miniprogram/toast/index';
import {
  login
} from '../../services/user/user';

const menuData = [
  [{
      title: '订单',
      tit: '50',
      url: '',
      type: 'order',
    },
    {
      title: '关注',
      tit: '23',
      url: '',
      type: 'focus',
    },
    {
      title: '我的名片',
      tit: '',
      url: '',
      type: 'business-card',
    },
    {
      title: '帮助中心',
      tit: '',
      url: '',
      type: 'service',
      icon: 'service',
    }
  ]
];

const orderTagInfos = [{
    title: '游览量',
    iconName: 'wallet',
    orderNum: 10,
    tabType: 5,
    status: 1,
  },
  {
    title: '待支付',
    iconName: 'deliver',
    orderNum: 0,
    tabType: 10,
    status: 1,
  },
  {
    title: '待确认',
    iconName: 'package',
    orderNum: 0,
    tabType: 40,
    status: 1,
  },
  {
    title: '待评价',
    iconName: 'comment',
    orderNum: 0,
    tabType: 60,
    status: 1,
  },
  {
    title: '退款/售后',
    iconName: 'exchang',
    orderNum: 0,
    tabType: 0,
    status: 1,
  },
];


Page({
  data: {
    showMakePhone: false,
    userInfo: {
      avatarUrl: '',
      nickName: '正在登录...',
      phoneNumber: '',
    },
    menuData,
    orderTagInfos,
    customerServiceInfo: {},
    currAuthStep: 1,
    showKefu: true,
    versionNo: ''
  },

  onLoad() {
    this.getVersionInfo();
    if (getApp().globalData.userInfo == null) {
      console.log(1111);
      this.login();
    } else {
      let globalInfo = getApp().globalData.userInfo
      this.setData({
        userInfo: {
          avatarUrl: globalInfo.avatarUrl,
          nickName: globalInfo.userName,
          userId: globalInfo.userId
        }
      })
      console.log(this.data.userInfo)
    }

  },

  onShow() {
    this.getTabBar().init();
    this.init();
  },
  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.fetUseriInfoHandle();
  },

  fetUseriInfoHandle() {},

  onClickCell({
    currentTarget
  }) {
    const {
      type
    } = currentTarget.dataset;

    switch (type) {
      case 'order': {
        wx.navigateTo({
          url: '/pages/usercenter/order/index'
        });
        break;
      }
      case 'focus': {
        wx.navigateTo({
          url: '/pages/usercenter/focus/index'
        });
        break;
      }
      case 'business-card': {
        wx.navigateTo({
          url: '/pages/usercenter/businessCard/index'
        });
        break;
      }
      case 'service': {
        this.openMakePhone();
        break;
      }
      case 'help-center': {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '你点击了帮助中心',
          icon: '',
          duration: 1000,
        });
        break;
      }
      case 'point': {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '你点击了积分菜单',
          icon: '',
          duration: 1000,
        });
        break;
      }
      case 'coupon': {
        wx.navigateTo({
          url: '/pages/coupon/coupon-list/index'
        });
        break;
      }
      default: {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '未知跳转',
          icon: '',
          duration: 1000,
        });
        break;
      }
    }
  },

  jumpNav(e) {
    const status = e.detail.tabType;

    if (status === 0) {
      wx.navigateTo({
        url: '/pages/order/after-service-list/index'
      });
    } else {
      wx.navigateTo({
        url: `/pages/order/order-list/index?status=${status}`
      });
    }
  },

  jumpAllOrder() {
    wx.navigateTo({
      url: '/pages/order/order-list/index'
    });
  },

  openMakePhone() {
    this.setData({
      showMakePhone: true
    });
  },

  closeMakePhone() {
    this.setData({
      showMakePhone: false
    });
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.customerServiceInfo.servicePhone,
    });
  },

  gotoUserEditPage() {
    wx.navigateTo({
      url: '/pages/usercenter/person-info/index'
    });
  },

  getVersionInfo() {
    const versionInfo = wx.getAccountInfoSync();
    const {
      version,
      envVersion = __wxConfig
    } = versionInfo.miniProgram;
    this.setData({
      versionNo: envVersion === 'release' ? version : envVersion,
    });
  },

  login() {
    console.log("login")
    wx.login({
      success: (result) => {
        console.log(result)
        if (result.code) {
          login(result.code).then((res) => {
            console.log(res)
            getApp().globalData.userInfo = res;
            this.setData({
              userInfo: {
                avatarUrl: res.avatarUrl,
                nickName: res.userName,
                userId: res.userId
              }
            })
          });
        }
      }
    })
  }
});